
package ae.pcfc.etks.online.web.controller.user.general.role.request;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.util.WebUtils;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.ConstantsCommon.WFProcessType;
import ae.pcfc.etks.online.common.delegate.ClientRegistrationDelegate;
import ae.pcfc.etks.online.common.util.CommonUtils;
import ae.pcfc.etks.online.common.vo.AppMetaDataDetailVO;
import ae.pcfc.etks.online.common.vo.ClientRoleVO;
import ae.pcfc.etks.online.web.ConstantsAdmin;
import ae.pcfc.etks.online.web.PageType;
import ae.pcfc.etks.online.web.ViewPath;
import ae.pcfc.etks.online.web.ViewUtils;
import ae.pcfc.etks.online.web.common.Constants;
import ae.pcfc.etks.online.web.controller.BaseController;
import ae.pcfc.etks.online.web.model.UserRoleRequest;
import ae.pcfc.etks.online.web.util.SecurityUtils;

@Controller
@RequestMapping(ViewPath.USER_ROLE_REQUEST)
public class UserRoleRequestController extends BaseController{

	private ClientRegistrationDelegate clientRegistrationDelegate;

	@Resource(name = "clientRegistrationDelegate")
	public void setClientRegistrationDelegate(ClientRegistrationDelegate clientRegistrationDelegate) {
		this.clientRegistrationDelegate = clientRegistrationDelegate;
	}
	
//	@ModelAttribute(value = Constants.USER_ROLE_REQUEST_MODEL_KEY)
//	public UserRoleRequest populateReport(HttpServletRequest request, @Valid UserRoleRequest userRole,
//			@RequestParam(value = "id", required = false) String id) {
//
//		// UserRoleRequestVO roleRequestVO =
//		// serviceDelegate.getRolesByApplicationId(new Long(id));
//		UserRoleRequestVO userRoleRequestVO = new UserRoleRequestVO();
//		List<UserRoleRequestVO> userRoleRequest;
//
//		/*
//		 * if( roleRequestVO != null ) { userRoleRequest = new
//		 * ArrayList<UserRoleRequestVO>(); userRoleRequestVO.setId(true);
//		 * userRoleRequestVO.setStrRoleAr("");
//		 * userRoleRequestVO.setStrRoleEng("");
//		 * userRoleRequest.add(userRoleRequestVO);
//		 * userRole.setArrRoleRequests(userRoleRequest);
//		 * 
//		 * }
//		 */
//
//		return new UserRoleRequest();
//
//	}
	
	@ModelAttribute(ConstantsAdmin.USER_ROLE_LIST_MODEL_KEY)
	public List<ClientRoleVO>  populateNonClientRolesListModel() {

		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String strUserName = null;

		if (principal instanceof UserDetails) {
			strUserName = ((UserDetails) principal).getUsername();
		} else {
			strUserName = principal.toString();
		}
		
		return this.clientRegistrationDelegate.findPersonAvailableRoleByUserName(strUserName);
	}

	@ModelAttribute(ConstantsAdmin.USER_CURRENT_ROLE_LIST_MODEL_KEY)
	public List<ClientRoleVO>  populateCurrerntRolesListModel() {

		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String strUserName = null;

		if (principal instanceof UserDetails) {
			strUserName = ((UserDetails) principal).getUsername();
		} else {
			strUserName = principal.toString();
		}
		
		return this.clientRegistrationDelegate.findCurrentRolesByUserName(strUserName);
	}
	/**
	 * Custom handler for displaying the form, i.e.: GET requests for
	 * /user/general/role/request/form.jsp
	 * 
	 * @see WEB-INF/jsp/user/general/role/request/form.jsp
	 */

	@RequestMapping(value = "/form.htm", method = RequestMethod.GET)
	public void form(@ModelAttribute(value = ConstantsAdmin.USER_ROLE_REQUEST_MODEL_KEY) UserRoleRequest command) {
		
	}

	/**
	 * Custom handler for displaying the form, i.e.:POST requests for
	 * user/general/role/request/form.jsp
	 * 
	 * @see #form(UserRoleRequest)
	 * @see WEB-INF/jsp/user/general/role/request/form.jsp
	 */

	@RequestMapping(method = RequestMethod.POST, value = "/form.htm")
	public String find(HttpServletRequest request,
			HttpSession session,
			SessionStatus status,
			@ModelAttribute(value = ConstantsAdmin.USER_ROLE_REQUEST_MODEL_KEY) @Valid UserRoleRequest userRoleRequest,
			BindingResult errors)  {

		boolean isSave = WebUtils.hasSubmitParameter(request, ConstantsAdmin.SAVE_BUTTON);

		if (isSave) 
		{
			if (errors.hasErrors()) 
			{
				return ViewUtils.DEFAULT_VIEW;
			}
			
			Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			String strUserName = null;

			if (principal instanceof UserDetails) {
				strUserName = ((UserDetails) principal).getUsername();
			} else {
				strUserName = principal.toString();
			}
			
			AppMetaDataDetailVO appMetaDataVO = new AppMetaDataDetailVO();

			
			appMetaDataVO.setApplicationDate(CommonUtils.getCurrentDateAsDDMMYYYY());
			appMetaDataVO.setStrWFinitatedBy(strUserName);
			appMetaDataVO.setStrStatusDescEn(getMessageSource().getMessage("msg.workflow.status.in.progress", null, Locale.ENGLISH));
			appMetaDataVO.setStrStatusDescAr(getMessageSource().getMessage("msg.workflow.status.in.progress", null, new Locale(Constants.LOCALE_ARABIC)));
			appMetaDataVO.setStrWfStatus(ConstantsCommon.IN_PROGRESS);
			//appMetaDataVO.setStrProcessCode(ConstantsAdmin.PROCESS_CODE_CLIENT_ROLE_REGISTRATION);
			appMetaDataVO.setProcessCode(WFProcessType.CLIENT_ROLE_REQUEST);
			appMetaDataVO.setStrProcessStep(ConstantsAdmin.PROCESS_STEP_INITIATE_CLIENT_ROLE_REGISTRATION);
			appMetaDataVO.setStrClientRefNo(ConstantsAdmin.ROLE);
			
			String strRefNo = this.clientRegistrationDelegate.addClientRoleReq(strUserName, userRoleRequest.getRoleIds(), appMetaDataVO);
			
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put(ConstantsAdmin.REFRENCE_NO, SecurityUtils.encryptQueryParam(strRefNo));

			session.setAttribute(ConstantsAdmin.SUCCESS_PARAM, ConstantsAdmin.SUCCESS_PARAM +" "+ strRefNo);
			
			return ViewUtils.getRedirect(ViewPath.USER_ROLE_REQUEST, PageType.FORM, paramMap);

			
		}
		return ViewUtils.DEFAULT_VIEW;
	}

}
